﻿using System;
using System.Timers;
using Reactor.Environment;
using Reactor.Messages.Events.Health;
using Reactor.ServiceGrid.ServiceEntities;

namespace Reactor.Services.PulseService
{
    public class PulseService : ManagedReactorService
    {
        private Timer _timer;

        #region Overrides of Base Types

        /// <summary>
        /// Called when the service is started.
        /// </summary>
        protected override void OnStarted()
        {
            _timer = new Timer(10000);
            _timer.Elapsed += Timer_Elapsed;
            _timer.Start();
        }

        /// <summary>
        /// Called when the instance is stopping.
        /// </summary>
        protected override void OnStopping()
        {
            base.OnStopping();
            _timer.Stop();
            _timer.Elapsed -= Timer_Elapsed;
            _timer.Dispose();
        }

        #endregion

        private static void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            Context.ServiceBus.Send(new Pulse { Message = string.Format("Current date and time: {0}", DateTime.Now) });
        }
    }
}
